﻿using System;
using System.Drawing;
using System.Windows.Forms;

namespace Fawcett.Plugins.Controls
{
    public partial class PFD : UserControl
    {
        private double _speed = 0; // Phi
        private double _PitchAngle = 0; // Phi
        private double _RollAngle = 0 * (Math.PI / 180); // Theta
        private double _heading = 10 * (Math.PI / 180);

        public double height { get; set; }
        public double heading
        {
            set
            {
                _heading = value * (Math.PI / 180);
                this.Refresh();
            }
        }
        public double speed 
        {
            set
            {
                _speed = 0 - value;
                this.Refresh();
            }
        }
        public double RollAngle 
        {
            set
            {
                _RollAngle = value * (Math.PI / 180);
                this.Refresh();
            }
        }
        public double PitchAngle
        {
            set
            {
                _PitchAngle = value ;
                this.Refresh();
            }
        }

        public PFD()
        {
            InitializeComponent();

            SetStyle(ControlStyles.DoubleBuffer | ControlStyles.UserPaint |    ControlStyles.AllPaintingInWmPaint, true);
        }

        #region Paint



        // Images
        private Bitmap bmpBackground = new Bitmap(Images.Primary_Flight_Display);
        private Bitmap bmpRoll = new Bitmap(Images.PFD_GroundSky);
        private Bitmap bmpASI = new Bitmap(Images.PFD_asi);
        private Bitmap bmpHeight = new Bitmap(Images.PFD_asi);
        private Bitmap bmpHeightGran = new Bitmap(Images.PFD_asi);
        private Bitmap bmpRose = new Bitmap(Images.Heading);
        private Bitmap bmpAvion = new Bitmap(Images.Maquette_Avion);

        protected override void OnPaint(PaintEventArgs pe)
        {
            // Calling the base class OnPaint
            base.OnPaint(pe);

            // Pre Display computings

            Point ptRoll = new Point(-25, -116);
            Point ptRotation = new Point(240, 230);
            Point ptASI = new Point(0, -210);
            Point ptHeight = new Point(400, -210);
            Point ptHeightGran = new Point(480, -210);

            float scale = (float)this.Width / bmpBackground.Width ;

            bmpBackground.MakeTransparent(Color.Yellow);
            bmpAvion.MakeTransparent(Color.Yellow);

            Common.RotateAndTranslate(pe, bmpRoll, _RollAngle, 0, ptRoll, (int)(4 * _PitchAngle), ptRotation, scale);

            Common.RotateAndTranslate(pe, bmpASI, 0.0, 0, ptASI, 0, ptRotation, scale);

            Common.RotateAndTranslate(pe, bmpHeight, 0.0, 0, ptHeight, 0, ptRotation, scale);

            Common.RotateAndTranslate(pe, bmpHeightGran, 0.0, 0, ptHeightGran, 0, ptRotation, scale);

            pe.Graphics.DrawImage(bmpRose,
                /// FIX ME
                (float) (_speed *4.5)- 125 ,
                (float)((0.5 * (bmpBackground.Height - 10) - 0.5 * bmpRose.Height) * scale),
                (float)((bmpRose.Width  ) * scale),
                (float)((bmpRose.Height  ) * scale));

            // diplay mask
          //  Pen maskPen = new Pen(this.BackColor, 40*  scale);
          //  pe.Graphics.DrawRectangle(maskPen, 0, 0, (float)(bmpBackground.Width * scale), (float)(bmpBackground.Height * scale));

            pe.Graphics.DrawImage(bmpBackground, 0, 0, (float)(bmpBackground.Width * scale), (float)(bmpBackground.Height* scale ));

            pe.Graphics.DrawImage(bmpAvion, 
                (float)((0.5 * ( bmpBackground.Width -100) - 0.5 * bmpAvion.Width) * scale ),
                (float)((0.5 * ( bmpBackground.Height -40) - 0.5 * bmpAvion.Height) * scale ),
                (float)((bmpAvion.Width +30) * scale ),
                (float)((bmpAvion.Height+30) * scale ));

  

        }



        #endregion
    }
}
